angular.module('starter.controllers', [])

  .controller('AppCtrl', function ($scope, $ionicModal, $timeout) {


  })

  .controller('HomeCtrl', function ($scope) {


  })


  .controller("MeCtrl", function ($scope, $http) {

    getPostsCountUrl = IP_ADDRESS+"/st-core/user!getMyPostsCount.action?user.id=1&callback=JSON_CALLBACK";

    $http.jsonp(getPostsCountUrl).success(
      function (data) {

        $scope.postsCount = data.response;
      }
    ).error(function () {

      console.log("获得用户发帖数量失败，url", getPostsCountUrl);
    });

    getMyPostsUrl = IP_ADDRESS+"/st-core/user!getMyPosts.action?user.id=1&callback=JSON_CALLBACK";

    $http.jsonp(getMyPostsUrl).success(
      function (data) {

        console.log(data.response);

        $scope.myPosts = data.response;

      }
    ).error(function () {

      console.log("获得用户帖子失败，url", getPostsCountUrl);

    });

  })

  .controller('LoginCtrl', function ($scope, $http,$state) {

    $scope.doLogin = function () {

      console.log('Doing login', $scope.user);

      loginUrl = IP_ADDRESS+"/st-core/user!login.action?user.username=" + $scope.user.username +
        "&user.userpwd=" + $scope.user.userpwd + "&callback=JSON_CALLBACK";


      $http.jsonp(loginUrl).success(
        function (data) {

          console.log(data);
          if (data.success) {

              console.log("data",data);

              user  = data.response.user;

              console.log("user",user);


            $state.go("app.homepage");

          } else {

            alert("密码错误,请重新登录!");
          }
        }
      ).error(function () {

        alert('失败');
      });
    };

  })
